package com.gatesim;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.Log;

public class NotGate extends Gate{
	
	private static int initResource = 0;
	//dist from top corner
	private static int ANDGATE_TOPPIN;
	private static int ANDGATE_BOTPIN;
	private static int ANDGATE_FNTPIN;
	private static Bitmap andPic; 
	private static Bitmap andPicSelected;
    private static Bitmap andPicCharged;
    private Bitmap picToDraw;
    private Paint paint;
	private static int and_width;
	private static int and_height;
	
	protected NotGate(Point c) {
		super(c);
		
		if(initResource == 0){
			initAnd();
			initResource++;
		}	

		corner.set(center.x - (and_width/2), center.y - (and_height/2) );
		calcPins();
		paint = new Paint();
		
//		center.set((center.x - center.x/4), center.y);
		
	    picToDraw = andPic;
        width  = and_width;
        height = and_height;
	}
	
	public static void initAnd(){
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inTempStorage = new byte[16*1024];
        
		//Load image, 1x
		andPic = BitmapFactory.decodeResource(resources, R.drawable.not, options);
		andPicSelected = BitmapFactory.decodeResource(resources, R.drawable.not_selected, options);
		andPicCharged = BitmapFactory.decodeResource(resources, R.drawable.not_charged, options);
		//Set Constants
		ANDGATE_TOPPIN = andPic.getHeight()/2; 
//		ANDGATE_BOTPIN = ANDGATE_TOPPIN; 
		ANDGATE_FNTPIN = andPic.getHeight()/2;
		//Will be the same for each object bc picture is the same.
		and_width  =  andPic.getWidth();
		and_height = andPic.getHeight();
		
	}
	
	private void calcPins(){
		topPin = new InputConnection(new Point(corner.x, corner.y + ANDGATE_TOPPIN));
	//	botPin = new InputConnection(new Point(corner.x,corner.y + ANDGATE_BOTPIN));
		fntPin = new OutputConnection(new Point(corner.x + and_width,corner.y + ANDGATE_FNTPIN));
	}
	
	public void recalcPins(){
		topPin.setPoint(corner.x, corner.y + ANDGATE_TOPPIN);
//		botPin.setPoint(corner.x,corner.y + ANDGATE_BOTPIN);
		fntPin.setPoint(corner.x + and_width,corner.y + ANDGATE_FNTPIN);		
	}
	

	@Override
	public int produceOut(){ 
		// And its children together
		int out = 1;
        
//		i = new Integer(i.intValue() + delayValue);
		
		for(Wire w: parents){
			if(w == null){
				fntPin.setCharge(Connection.CONN_NO_CHARGE);
				break;
			} else {
				w.produceOut(); //getGateSource().produceOut();//i);
				w.relayCharge();
				
				if ( GateWorld.animationMode && (this.personalDelay < (w.personalDelay + this.delayValue)) ){
				    personalDelay = new Integer(w.personalDelay + this.delayValue);
				}
				
				
				if( w.getDest().getCharge() == Connection.CONN_NO_CHARGE || 
		    	                        out == Connection.CONN_NO_CHARGE) {
					out = Connection.CONN_NO_CHARGE;
				} else {
					out = (w.getDest().getCharge() == 0) ? 1 : 0;
				}
				fntPin.setCharge(out);
			}
		}
		
//        personalDelay = new Integer(personalDelay.intValue() + delayValue);
//        if ( personalDelay.intValue() < i.intValue() ){
//          personalDelay = new Integer(i.intValue());
//        }

//		if(GateWorld.animationMode){
	        if ( _gCount == personalDelay.intValue() ){ //i.intValue() ){
	    		if(fntPin.getCharge() == Connection.CONN_ONE_CHARGE){
	    			picToDraw = andPicCharged;
	    		}else{
	    		    picToDraw = andPic;
	    		}
	    		_gCount = 0;
			}
		    _gCount++;
//		}

		return fntPin.getCharge();
	}
	
	public void endSimulationMode(){
		picToDraw = andPic;
	    _gCount = 0;
	    personalDelay = new Integer(0);
	}

	@Override
	public void draw(Canvas c,Resources r, boolean simulationMode) {
		c.drawBitmap(picToDraw, corner.x, corner.y, null);		
		fntPin.draw(c, r);
		topPin.draw(c, r);
//		botPin.draw(c, r);
		if(simulationMode) {
			paint.setTextSize(50);
			paint.setColor(Color.RED);
			c.drawText(""+((fntPin.getCharge() == Connection.CONN_NO_CHARGE) ? "X" : fntPin.getCharge()), 
					fntPin.getPoint().x + 20, fntPin.getPoint().y, paint);
		}
	}
	
	@Override
	public int getWidth(){
		return and_width;
	}
	@Override
	public int getHeight(){
		return and_height;
	}

   @Override
    public void select(boolean b, boolean simulationMode){
	   if(!simulationMode){
		   picToDraw = (b) ? andPicSelected : andPic; 
	   }
    }
   
    @Override
    public boolean addTopParent(Wire p){
       if(parents[0] == null || !topPin.isOccupied()){
           parents[0] = p;
           parents[1] = p;
           return true;
       }
       return false;
    }
   
    @Override
    public boolean addBotParent(Wire p){return false;}
    
    @Override
    public boolean addParent(Wire p){
        if(this.topPin.isSelected()){
            return addTopParent(p);
        }
        return false;
    }
}
